package red;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class InputUrnaJob extends InputJob {

	private JLabel label;

	public InputUrnaJob() {
		super();
	}

	public void setStateLabel(JLabel label) {
		this.label = label;
	}

	public InputUrnaJob(InputStream in) {
		super(in);
	}

	private void changeLabel(final String text) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				label.setText(text);
			}
		});
	}

	@Override
	public void run() {
		if (in != null) {
			InputStreamReader reader = new InputStreamReader(in);
			BufferedReader bufferedReader = new BufferedReader(reader);
			try {
				if (bufferedReader.readLine().equals("OK")) {
					this.changeLabel("Datos Enviados Correctamente");
				}
			} catch (IOException e) {
				System.out.println(e.getMessage());
				if (e.getMessage().equals("Read timed out")) {
					this.changeLabel("No se pudieron enviar los datos");
				}
			}
		}
		// TODO Auto-generated method stub
	}

}
